package com.morgan.test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CTFuture {
    
    public static void main (String[] args) throws InterruptedException, ExecutionException {
        final ExecutorService exec = Executors.newFixedThreadPool(5);
        Callable<String> call = new Callable<String>() {
            public String call() throws Exception {
                Thread.sleep(1000 * 5);
                return "Other less important but long time things";
            }
        };
        
        long firstTimeMillis = System.currentTimeMillis();
        Future<String> task = exec.submit(call);
        
        long secondTimeMillis = System.currentTimeMillis();
        System.out.println(secondTimeMillis - firstTimeMillis);
        
        Thread.sleep(1000 * 3);
        System.out.println("Let's do important thing.");
        
        long thirdTimeMillis = System.currentTimeMillis();
        System.out.println(thirdTimeMillis - secondTimeMillis);
        
        String obj = task.get();
        System.out.println(obj);
        
        exec.shutdown();
        
        long fourthTimeMillis = System.currentTimeMillis();
        System.out.println(fourthTimeMillis - thirdTimeMillis);
    }

}
